package com.sunday.common.mq.kafka.study.spring.e4_Routing_Kafka_Template;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.KafkaException;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.kafka.core.*;
import org.springframework.kafka.support.KafkaHeaders;
import org.springframework.kafka.support.SendResult;
import org.springframework.messaging.support.GenericMessage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.*;

/**
 * https://docs.spring.io/spring-kafka/docs/current/reference/html/#routing-template
 * {@link org.springframework.boot.autoconfigure.kafka.KafkaAutoConfiguration}
 */
@Slf4j
@RestController
@RequestMapping("/test")
public class MQApi {

    @Autowired
    private RoutingKafkaTemplate routingTemplate;

    @GetMapping("/send")
    public void send(@PathVariable String key) throws ExecutionException, InterruptedException {
        CompletableFuture<SendResult<Object, Object>> future;
        future = routingTemplate.send("topic3", "thing1");
        log.info("{}", future.get());
        future = routingTemplate.send("topic2", "thing2".getBytes());
        log.info("{}", future.get());
    }

    @KafkaListener(id = "myId_2", topics = "topic2")
    public void listen_2(String in) {
        log.info("[myId_2]======= {}", in);
    }

    @KafkaListener(id = "myId_3", topics = "topic3")
    public void listen_3(String in) {
        log.info("[myId_3]======= {}", in);
    }

}
